<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Library\Logic\KefuLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopUserLogic;
use App\Library\Traits\AdminControllerTrait;
use App\Models\KefuModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;

class KefuController extends AdminController
{
    use AdminControllerTrait;

    protected $title = '客服';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new KefuModel);
        $grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'kefu.user_id')
            ->select('kefu.*', 'shop_user.user_mobile')
            ->orderByDesc('kefu.created_at');
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('kefu-id', 'ID')->integer();
                $filter->equal('kefu-status', __('l.status'))->select(__('kefu.statusArr'));
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->like('product_name', '名称');
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->column('status', __('l.status'))->using(__('kefu.statusArr'));
        $grid->column('user_id', '用户ID');
        $grid->column('file_id', __('l.fileId'))->display(function ($field) {
            return is_null($field) ? null : fileIdToUrl($field);
        })->image(config('oss.url'), 60, 60);
        $grid->column('name', '名称');
        $grid->column('user_mobile', __('l.user_mobile'));
        $grid->column('created_at', __('l.createdAt'));
        $grid->disableCreateButton(false);
        $grid->actions(function (Actions $actions) {
            $actions->disableEdit(false);
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->disableActions(false);
        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new KefuModel);
        $form->hidden('file_id');
        $form->hidden('seller_id');
        $form->hidden('status')->default(0);
        $form->hidden('ossPrefix')->default(config('oss.url'));
        $form->ignore(['ossPrefix']);
        $form->setWidth(10);
        if ($form->isEditing()) $form->hidden('user_id');
        $form->column(1 / 2, function (Form $form) {
            if ($form->isCreating()) $form->number('user_id', '用户ID')->attribute('maxlength', 10)->required();
            $form->text('name', '名称')->default(random())->required();
        });
        $form->column(1 / 2, function (Form $form) {
            $form->display('file_id', __('l.fileId'))->with(function () {
                return view('upload_image');
            });
        });
        $form->saving(function (Form $form) {
            $userId = Admin::user()->user_id;
            $mainId = ShopUserLogic::valueByWhere('userId', $userId, 'userMainId');
            $form->seller_id = is_null($mainId) || $mainId === 0 ? $userId : $mainId;
            if ($form->isCreating()) {
                if (!ShopUserLogic::existsByWhere('userId', $form->user_id)) return $this->backWith('用户ID不存在');
                else if (cache()->has('kf:' . $form->user_id)) return $this->backWith('客服已经存在');
                $mainId = ShopUserLogic::valueByWhere('userId', $form->user_id, 'userMainId');
                if (is_null($mainId)) return $this->backWith('用户ID不符合添加条件,必须是员工');
                if (config('app.superId') !== $mainId) return $this->backWith('该员工不属于当前管理员');
            }
            return null;
        });
        $form->saved(function (Form $form) {
            OperateLogLogic::add(Admin::user()->user_id);
            cache(['kf:' . $form->user_id => [
                'status' => (int)$form->status,
                'sellerId' => (int)$form->seller_id,
                'fileId' => $form->file_id,
                'name' => $form->name,
            ]]);
        });
        return $form;
    }
}
